<?php
namespace App\Models\SalesReportLT;

use App\Models\Product\ProductClassify;
use Illuminate\Database\Eloquent\Model;

class PaymentCNSummary extends Model
{
    protected $table = 'lt_sr_payment_none_classify';

    protected $guarded = [];

    public $timestamps = false;

    public function classify(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo(ProductClassify::class,'classify_id');
    }

    public static function headersField(): array
    {
        return self::$_basic +self::$_common;
    }

    public static $_basic = [
        'season' => ['title' => '季节性','sumField' => false,'per' => false],
        'classify' => ['title' => '品类','sumField' => false,'per' => false]
    ];

    public static $_common = [
        'type' => ['title' => '值类型','sumField' => false,'per' => false],
//        'rate_odds' => ['title' => '汇率','sumField' => false,'per' => false],
        'sales_volume' => ['title' => '销量','sumField' => true,'per' => false],
        'sales_amount_rmb' => ['title' => '销售额RMB','sumField' => true,'per' => false],
        'refund_num' => ['title' => '退货数量','sumField' => true,'per' => false],
        'refund_amount_rmb' => ['title' => '退款金额RMB','sumField' => true,'per' => true],
        'thfba_rmb' => ['title' => '三方发货费RMB','sumField' => true,'per' => true],
        't_purchase_price_rmb' => ['title' => '总采购成本RMB','sumField' => true,'per' => true],
        't_shipment_price_rmb' => ['title' => '总运费成本RMB','sumField' => true,'per' => true],
        'other_fee_rmb' => ['title' => '额外费用RMB','sumField' => true,'per' => true],
        'system_use_pc_fee_rmb' => ['title' => '系统利用费_PCRMB','sumField' => true,'per' => true],
        'system_use_sp_fee_rmb' => ['title' => '系统利用费_SPRMB','sumField' => true,'per' => true],
        'system_use_c_fee_rmb' => ['title' => '系统利用费_共通RMB','sumField' => true,'per' => true],
        'super_membership_rewards_fee_rmb' => ['title' => '超级会员_成果报酬RMB','sumField' => true,'per' => true],
        'super_membership_system_use_fee_rmb' => ['title' => '超级会员_系统利用费RMB','sumField' => true,'per' => true],
        'super_membership_selling_fee_rmb' => ['title' => '超级会员_佣金RMB','sumField' => true,'per' => true],
        'super_membership_selling_re_fee_rmb' => ['title' => '超级会员_佣金减免RMB','sumField' => true,'per' => true],
        'integral_pc_fee_rmb' => ['title' => '积分_PCRMB','sumField' => true,'per' => true],
        'integral_sp_fee_rmb' => ['title' => '积分_SPRMB','sumField' => true,'per' => true],
        'rakuten_pay_use_fee_rmb' => ['title' => 'RakutenPay利用费RMB','sumField' => true,'per' => true],
        'rakuten_pay_visa_fee_rmb' => ['title' => 'RakutenPay_VISA支付费RMB','sumField' => true,'per' => true],
        'service_fee_rmb' => ['title' => '服务费RMB','sumField' => true,'per' => true],
        'spend_rmb' => ['title' => '广告费用合计RMB','sumField' => true,'per' => true],
        'reimburse_rmb' => ['title' => '刷单费用RMB','sumField' => true,'per' => true],
        'reimburse_os_rmb' => ['title' => '运营服务费RMB','sumField' => true,'per' => true],
        'reimburse_profit_rmb' => ['title' => '刷单毛利扣减RMB','sumField' => true,'per' => true],
        'profit_rmb' => ['title' => '毛利值RMB','sumField' => true,'per' => true],
        'profit_rate' => ['title' => '毛利率','sumField' => false,'per' => false],
        'earning_rmb' => ['title' => '销售收入RMB','sumField' => true,'per' => true],
        'total_rmb' => ['title' => 'TotalRMB','sumField' => true,'per' => true],
        'avg_price' => ['title' => '平均售价','sumField' => false,'per' => false],
    ];

    //数值+占比
    public static function getDownloadData($re): \Generator
    {
        foreach ($re as $row){
            yield self::getDataBasic($row) + self::getNumDataCommon($row);
            yield self::getDataBasic($row) + self::getPerDataCommon($row);
        }
    }
    //数值
    public static function getDownloadNumData($re): \Generator
    {
        foreach ($re as $row){
            yield self::getDataBasic($row) + self::getNumDataCommon($row);
        }
    }
    //占比
    public static function getDownloadPerData($re): \Generator
    {
        foreach ($re as $row){
            yield self::getDataBasic($row) + self::getPerDataCommon($row);
        }
    }

    public static function getDataBasic($row): array
    {
        return [
            ProductClassify::$_season[$row->classify->season ?? 3],
            $row->classify->title ?? ''
        ];
    }
    public static function getNumDataCommon($row): array
    {
        return [
            10 => '数值',
            11 => $row->sales_volume,
            12 => $row->sales_amount_rmb,
            13 => $row->refund_num,
            14 => $row->refund_amount_rmb,
            15 => $row->thfba_rmb,
            16 => $row->t_purchase_price_rmb,
            17 => $row->t_shipment_price_rmb,
            18 => $row->other_fee_rmb,
            19 => $row->system_use_pc_fee_rmb,
            20 => $row->system_use_sp_fee_rmb,
            21 => $row->system_use_c_fee_rmb,
            22 => $row->super_membership_rewards_fee_rmb,
            23 => $row->super_membership_system_use_fee_rmb,
            24 => $row->super_membership_selling_fee_rmb,
            25 => $row->super_membership_selling_re_fee_rmb,
            26 => $row->integral_pc_fee_rmb,
            27 => $row->integral_sp_fee_rmb,
            28 => $row->rakuten_pay_use_fee_rmb,
            29 => $row->rakuten_pay_visa_fee_rm,
            30 => $row->service_fee_rmb,
            31 => $row->spend_rmb,
            32 => $row->reimburse_rmb,
            33 => $row->reimburse_os_rmb,
            34 => $row->reimburse_profit_rmb,
            35 => $row->profit_rmb,
            36 => getPercentage($row->profit_rmb,$row->sales_amount_rmb),
            37 => $row->earning_rmb,
            38 => $row->total_rmb,
            39 => intval($row->sales_volume) > 0 ? round(($row->sales_amount_rmb) / intval($row->sales_volume),3) : '--',
        ];
    }

    public static function getPerDataCommon($row): array
    {
        return [
            10 => '占比',
            11 => '--',
            12 => '--',
            13 => getPercentage($row->refund_num,$row->sales_volume),
            14 => getPercentage($row->refund_amount_rmb,$row->sales_amount_rmb),
            15 => getPercentage($row->thfba_rmb,$row->sales_amount_rmb),
            16 => getPercentage($row->t_purchase_price_rmb,$row->sales_amount_rmb),
            17 => getPercentage($row->t_shipment_price_rmb,$row->sales_amount_rmb),
            18 => getPercentage($row->other_fee_rmb,$row->sales_amount_rmb),
            19 => getPercentage($row->system_use_pc_fee_rmb,$row->sales_amount_rmb),
            20 => getPercentage($row->system_use_sp_fee_rmb,$row->sales_amount_rmb),
            21 => getPercentage($row->system_use_c_fee_rmb,$row->sales_amount_rmb),
            22 => getPercentage($row->super_membership_rewards_fee_rmb,$row->sales_amount_rmb),
            23 => getPercentage($row->super_membership_system_use_fee_rmb,$row->sales_amount_rmb),
            24 => getPercentage($row->super_membership_selling_fee_rmb,$row->sales_amount_rmb),
            25 => getPercentage($row->super_membership_selling_re_fee_rmb,$row->sales_amount_rmb),
            26 => getPercentage($row->integral_pc_fee_rmb,$row->sales_amount_rmb),
            27 => getPercentage($row->integral_sp_fee_rmb,$row->sales_amount_rmb),
            28 => getPercentage($row->rakuten_pay_use_fee_rmb,$row->sales_amount_rmb),
            29 => getPercentage($row->rakuten_pay_visa_fee_rmb,$row->sales_amount_rmb),
            30 => getPercentage($row->service_fee_rmb,$row->sales_amount_rmb),
            31 => getPercentage($row->spend_rmb,$row->sales_amount_rmb),
            32 => getPercentage($row->reimburse_rmb,$row->sales_amount_rmb),
            33 => getPercentage($row->reimburse_os_rmb,$row->sales_amount_rmb),
            34 => getPercentage($row->reimburse_profit_rmb,$row->sales_amount_rmb),
            35 => getPercentage($row->profit_rmb,$row->sales_amount_rmb),
            36 => '--',
            37 => getPercentage($row->earning_rmb,$row->sales_amount_rmb),
            38 => getPercentage($row->total_rmb,$row->sales_amount_rmb),
            39 => '--',
        ];
    }

    public static function _getFields($headers,$groupByArr): string
    {
        $re = '';
        foreach ($headers as $field => $item){
            if($item['sumField']){
                $re .= 'sum('.$field.') as '.$field.',';
            }
        }
        return $re.implode(',',$groupByArr);
    }
}
